﻿using EntityModel;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace API.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class RecordController : ControllerBase
    {
        private readonly BitDbContext _db;
        public RecordController(BitDbContext db)
        {
            _db = db;
        }
        /// <summary>
        /// 查询
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public Result GetFavor()
        {
            Result result = new Result();
            List<EM_Record> Favor = _db.EM_Records.ToList();
            if (Favor != null)
            {
                result.Code = 200;
                result.Data = Favor;
            }
            else
            {
                result.Code = 404;
            }
            result.Author = "";
            return result;
        }
        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="eM_Favorites"></param>
        /// <returns></returns>
        [HttpPost]
        public Result AddRecord([FromBody] EM_Record eM_Record)
        {
            Result result = new Result();
            _db.EM_Records.Add(eM_Record);
            result.Data = _db.SaveChanges();
            return result;
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpDelete]
        public Result DelUser(Guid id)
        {
            Result result = new Result();
            EM_Record? user = _db.EM_Records.Where(w => w.Id == id).SingleOrDefault();
            if (user != null)
                _db.EM_Records.Remove(user);
            result.Data = _db.SaveChanges();
            return result;
        }
    }
}
